package multicast.client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MulticastClient {
	
	private static String GROUP = "230.0.0.1";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		MulticastSocket socket = new MulticastSocket(4446);
		InetAddress address = InetAddress.getByName(GROUP);
		socket.joinGroup(address);

        DatagramPacket packet;
        int count = 0;
        while (count < 20) {
		    byte[] buf = new byte[256];
	        packet = new DatagramPacket(buf, buf.length);
	        socket.receive(packet);
	
	        String received = new String(packet.getData(), 0, packet.getLength());
	        count++;	        
	        System.out.println(count +": Mensagem recebida: " + received);
	        
		}
        
		socket.leaveGroup(address);
		socket.close();
		
	}
}
